<?php 

//########## STATIC METHOD CALLS ######################

//Autocomplete code like: 
//$foo = ClassRegistry::init("MyClassName");
//$foo->_
function findExpressionStaticMethodCallType($targetClassName, $methodName, $args) {
	//Find the type based on the parameter of a static method call:
	echo "Static $targetClassName $methodName {$args[0]}\n";
	
	if($targetClassName == 'ClassRegistry' && $methodName == 'init'
		&& count($args) == 1 && is_string($args[0])) {
		
			return $args[0];
	}
	return null; //I don't know
}

/**/


//########## INSTANCE METHOD CALLS #####################

//Autocomplete code like:
//$injector = new ClassRegistry();
//$foo = $injector->init("MyClassName");
//$foo->_
function findExpressionMethodCallType($targetClassNames, $methodName, $args) {
	//Find the type based on the first parameter of an instance method call:
	echo "Object " . join('|', $targetClassNames) . " $methodName " . join ('|', $args) . " \n";
	
	if(count($args) == 1 && is_string($args[0])) {
		//Look for one of in the type hierarchy:
		foreach($targetClassNames as $targetClassName) {
			if($targetClassName == 'ClassRegistry' && $methodName == 'init') {
					return $args[0];
			}
		}
	}
	return null;
}

/* or:
//Autocomplete code like:
//$injector = new ClassRegistry();
//$foo = $injector->initMyClassName();
//$foo->_
function findExpressionMethodCallType($targetClassNames, $methodName, $args) {
	//Find the type based on the method name:
	echo "Object " . join('|', $targetClassNames) . " $methodName " . join ('|', $args) . " \n";
	
	//Look for one of in the type hierarchy:
	foreach($targetClassNames as $targetClassName) {
		if($targetClassName == 'ClassRegistry' && strpos($methodName,'init') == 0) {
				return substr($methodName, 4);
		}
	}
	return null; //I don't know
}
*/

//########## OBJECT METHODS AND PROPERTIES #####################

//Autocomplete code like:
//$model = new Model_Foo();
//$model->_
function getInstanceMethodsCompletion($targetClassNames) {
	//Autocomplete the method and properties of a variable containing an instance of a specific class:
	echo "Methods for Object " . join('|', $targetClassNames) . "\n";

	//Look for one of in the type hierarchy:
	foreach($targetClassNames as $targetClassName) {
		if(strpos($targetClassName, 'Model_') === 0) { 
				return array('$userId', '$email', 'findAllByUserId', 'findAllByEmail', 'getComments');
		}
	}
	return null;
}

/* Reading your mysql db schema:
//Autocomplete code like:
//$model = new Model_MyTableOnTheDb();
//$model->_

//You even can mix the previous methods:
//$model = ClassRegistry::init("Model_MyTableOnTheDb");
//$model->_
function getInstanceMethodsCompletion($targetClassNames) {
	static $tables;
	if($tables == null) $tables = array();
	
	$CACHE_DURATION_SECONDS = 100; //query the dbevery 100 seconds
	echo "Methods for Object " . join('|', $targetClassNames) . "\n";

	//Look for one of in the type hierarchy:
	foreach($targetClassNames as $targetClassName) {
		if(strpos($targetClassName, 'Model_') === 0) { 
				$tableName = strtolower(substr($targetClassName, 6));

				if((time() - $tables[$tableName][0]) < $CACHE_DURATION_SECONDS) return $tables[$tableName][1];
				echo "Table: $tableName\n";
				
				mysql_connect('mysql.server.address', 'user', 'pass');
				mysql_select_db('database');
				$result = mysql_query("SHOW COLUMNS FROM $tableName");
				if($result === false) return null;
				
				$ret = array();
				if (mysql_num_rows($result) > 0) {
				    while ($field = mysql_fetch_assoc($result)) {
				    	$name = $field['Field'];
				       	$ret[] = '$' . $name;
						$ret[] = 'findAllBy' . ucfirst($name) . '($'. $name. ')';
				    }
				}
				$tables[$tableName] = array(time(), $ret);
				return $ret;
		}
	}
	return null;
}
